package Scuttlebutt;

import java.io.*;
import java.lang.*;

public class Settings extends Object implements Serializable 
{
	int x;
	int y;
	int width;
	int height;
	int divider;
	int lower_divider;
	int state;
	static File file = null;
	
	
	public Settings(){
		this.x = 133;
		this.y = 100;
		this.width = 532;
		this.height = 400;
		this.divider = 125;
		this.lower_divider = 250;
		file = new File(System.getProperty("user.dir") + File.separatorChar + "settings.dat");
	}
	
	public Settings(int x, int y, int width, int height, int divider, int lower_divider){
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.divider = divider;
		this.lower_divider = lower_divider;
		file = new File(System.getProperty("user.dir") + File.separatorChar + "settings.dat");
	}
	
	public void setWindowState(int state){
		this.state = state;
	}
	
	public int getWindowState(){
		return state;
	}
	
	public void setLowerDivider(int val){
		lower_divider = val;
	}
	
	public int getLowerDivider(){
		return lower_divider;
	}
	
	public void setDivider(int val){
		divider = val;
	}
	
	public int getDivider(){
		return divider;
	}
	
	static public Settings openSettings(){
		Settings s = null;
		file = new File(System.getProperty("user.dir") + File.separatorChar + "settings.dat");
    	try{
	    	ObjectInputStream st = new ObjectInputStream(new FileInputStream(file));
	    	s = (Settings)st.readObject();
		}catch(IOException io){
			s = create();
		}catch(ClassNotFoundException cnf){
			System.err.print(cnf + "\n");
		}
		return s;
	}
	
	public void setSettings(Scuttlebutt parent){
	    this.x = parent.getLocation().x;
	    this.y = parent.getLocation().y;
	    this.width = parent.getSize().width;
	    this.height = parent.getSize().height;
	}
	
    static public void saveSettings(Settings s){
		file = new File(System.getProperty("user.dir") + File.separatorChar + "settings.dat");
    	try{
	    	ObjectOutputStream st = new ObjectOutputStream(new FileOutputStream(file));
	    	st.writeObject(s);
		}catch(IOException io){
			System.err.print(io + "\n");
		}

    }   
    static public Settings create(){
		Settings s = new Settings();
    	try{
	    	ObjectOutputStream st = new ObjectOutputStream(new FileOutputStream(file));
	    	st.writeObject(s);
	    	st.close();
		}catch(IOException io){
			System.err.print(io + "\n");
		}
		return s;
    }   	
	
}

